// pages/login/login.js


var util= require('../../utils/util.js')
var _md = require("../../lib/md5.js");

var _md2 = _interopRequireDefault(_md);

var _util = require("../../lib/util_2");

var _util2 = _interopRequireDefault(_util);

function _interopRequireDefault(obj) {
    return obj && obj.__esModule ? obj : {
        default: obj
    };
}

var app = getApp();


Page({

  /**
   * 页面的初始数据
   */
  data: {
    phoneNum: "",
    regCode: "",
    isSendCodeButtonDisable: false,
    downCountTime: 60,
    isFirst: true,
    autoFocus: false,
    isBindButtonActive: false,
    isInputActive: true,
    isLoginSuccess: false,
    parameter: {
      'navbar': '1',
      'return': '1',
      'title':'绑定手机号'
    },
},
/**

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  getPhoneNumber:function(e){
    var detail = e.detail, cache_key = wx.getStorageSync('cache_key'),that=this;
    if (detail.errMsg =='getPhoneNumber:ok'){
      if (!cache_key){
        app.globalData.token='';
        app.globalData.isLog=false;
        return false;
      }
      app.basePost(app.U({ c: 'user_api', a: 'bind_mobile' }), { 
        iv: detail.iv,
        cache_key: cache_key, 
        encryptedData: detail.encryptedData
      },function(res){
        that.setData({ 'userInfo.phone': res.data.phone});
         app.Tips({ title: res.msg, icon: 'success' });
         app.globalData.userPhone = res.data.phone;
         setTimeout(function () { wx.navigateBack({ delta: 1 }) }, 1200)
       
      },function(res){
        return app.Tips({ title: '绑定失败'});
      });
    }else{
      app.Tips({ title:'取消授权'});
    }
  },

  countDown: function countDown() {
    var it = this;
    var theTime = +new Date() + it.data.downCountTime * 1e3;
    it.i = setInterval(function() {
        var now = +new Date();
        var diff = Math.floor((theTime - now) / 1e3);
        if (diff <= 0) {
            it.setData({
                isSendCodeButtonDisable: false,
                downCountTime: 60,
                //不是第一次发送验证验证码了，并在倒计时结束显示发送语音验证码
                isFirst: false
            });
            clearInterval(it.i);
        } else {
            it.setData({
                downCountTime: diff
            });
        }
    }, 300);
},
//发送验证码
_sendCode: function _sendCode(codeType) {
  var it =this
    //如果已经在请求验证码中，点击无效
    if (this.isSendCodeClick) {
        return;
    }
    if (!this.data.phoneNum) {
        wx.showToast({
            title: "手机号不能为空",
            icon: "none"
        });
    } else if (_util2.default.checkPhoneNum(this.data.phoneNum)) {
        //获取短信验证码
        if (codeType === "sms") {
          app.basePost(app.U({ c: 'public_api', a:'send_sms'}),{phone:this.data.phoneNum},function(res){
            if(res.code==200){
                    //短信发送成功
                  wx.showToast({
                    title: "发送成功"
                });
                if (codeType === "sms") {
                    it.setData({
                        isSendCodeButtonDisable: true,
                        downCountTime: 60
                    }, function() {
                        it.countDown();
                    });
                }
            } else{
              wx.showToast({
                title: "发送失败!",
                icon:"none"
              
            });
            it.setData({
              isSendCodeButtonDisable: true,
              downCountTime: 0
          }, function() {
              it.countDown();
          });
            }
            
          },function(res){
            wx.showToast({
              title: "发送失败!",
              icon:"none"
            });
          });
     
        }
        if (codeType === "voice") {
        }
    } else {
        wx.showToast({
            title: "手机号码错误",
            icon: "none"
        });
    }
},
onSMSSendCodeTap: function onSMSSendCodeTap() {
  if (!this.data.isSendCodeButtonDisable) {
      this._sendCode("sms");
  }
},
onVoiceSendCodeTap: function onVoiceSendCodeTap() {
  this._sendCode("voice");
},
//获取input value
onPhoneInput: function onPhoneInput(e) {
  this.setData({
      phoneNum: e.detail.value,
      isBindButtonActive: _util2.default.checkPhoneNum(e.detail.value) && _util2.default.checkRegCode(this.data.regCode)
  });
},
onRegCodeInput: function onRegCodeInput(e) {
  this.setData({
      regCode: e.detail.value,
      isBindButtonActive: _util2.default.checkPhoneNum(this.data.phoneNum) && _util2.default.checkRegCode(e.detail.value)
  });
},
onInputClick: function onInputClick(e) {
  var isPhone = e.currentTarget.dataset.is_phone;
  this.setData({
      isInputActive: isPhone
  });
},
onDeletePhone: function onDeletePhone() {
  this.setData({
      phoneNum: "",
      autoFocus: true,
      regCode: "",
      isBindButtonActive: false
  });
},
//绑定手机 用戶綁定手機號
bindGetUserInfo: function bindGetUserInfo(e) {

  
  var _this = this;
  var data = {
    phone:this.data.phoneNum, 
    code:this.data.regCode,
  }
  app.basePost(app.U({ c: 'public_api', a: 'vail_login' }), data, function (res) {
    if(res.code==200){
      wx.showToast({
        title: "登录成功",
        icon: "success"
    });
    app.globalData.userPhone = _this.data.phoneNum;
    setTimeout(function(){wx.navigateBack();},1000)
    
    }else{
      wx.showToast({
        title: "登录失败！",
        icon: "none"
    });
    }
    },function(res){
      wx.showToast({
        title: "登录失败！",
        icon: "none"
    });
    });
    return
},
 /**
   * 授权回调
  */
 onLoadFun:function(){
 
},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})